package alogrithm;

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.HashMap;
import java.util.Map;

/**
 * @author lbw
 * @version 1.0
 * @date 2021/10/18 17:54
 */
public class IsValid {
    public boolean isValid(String s) {
        // write code here

        Deque<Character> stack = new ArrayDeque<>();
        Map<Character, Character> map = new HashMap<Character, Character>() {{
            put('}', '{');
            put(']', '[');
            put(')', '(');
        }};
        for (char ch : s.toCharArray()) {
            if (ch == '(' || ch == '{' || ch == '[') {
                stack.push(ch);
            } else {
                if (map.containsKey(ch)) {
                    if (stack.isEmpty() && stack.pop() != map.get(ch)) {
                        return false;
                    }
                }
            }
        }
        return stack.isEmpty();
    }
}
